{include file="include/top"}
<style>
    td,.layui-table th{
        text-align: center;!important;
    }
</style>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">商品管理</a>
            <a>
              <cite>商品列表</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card" style="width: 100%;">
                <div class="layui-card-body ">
                    <div class="layui-row">
                        <form class="layui-form layui-col-md12 x-so " action="{:url('Goods/goodList')}" method="get">
                            <div class="layui-inline layui-show-xs-block" style="margin: 10px 10px 10px 0px;width: 120px;">
                                <select name="is_on_sale" lay-verify="">
                                    <option value="">全部</option>
                                    <option value="1">上架</option>
                                    <option value="2">下架</option>
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block" style="margin: 10px 10px 10px 0px;width: 120px;">
                                <select name="intro" lay-verify="">
                                    <option value="0">全部</option>
                                    <option value="go_recom">推荐</option>
                                    <option value="go_new">新品</option>
                                    <option value="go_hot">促销</option>
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block" style="margin: 10px 10px 10px 0px;width: 120px;">
                                <select name="xcinput" lay-verify="">
                                    <option value="go_sn">商品货号</option>
                                    <option value="go_name">商品名称</option>
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="keyword"  placeholder="请输入搜索内容" class="layui-input" style="width: 95%;margin: 10px 10px 10px 0px">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-btn" name="sub" type="submit" value="搜索" style="margin: 10px 10px 10px 0px">
                            </div>
                        </form>
                    </div>
                    <xblock>
                        <button class="layui-btn" onclick="edit()"><i class="layui-icon">&#xe654;</i>添加商品</button>
                    </xblock>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table" >
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>商品信息</th>
                            <th>商品价格</th>
                            <th>库存</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {volist name="list" id="data"}
                        <tr>
                            <td>{$key+1}</td>
                            <td>
                                <dl style="text-align:left">
                                    <dt>
                                        <a style="text-decoration:none" href="{$data.go_thumb}" target="_blank" title="查看">
                                            <img src="{$data.go_thumb}" width="65" height="65" style="float:left; border-radius: 5%;" />
                                        </a>
                                    </dt>
                                    <dd style="float: left; padding-left: 10px">{$data.go_name}<br>
                                        <span style="color:#999; font-size:12px;">{if $data.go_caid != ''}商品分类：{$data.ca_name}{/if}</span>
                                        <p><img src="__PUBLIC__/caidongweb/images/{if $data.go_sale == 1}sj1.png{else}sj2.png{/if}" onclick="changeTableValGoods('goods','go_id={$data.go_id}','go_sale','sj',this)"/>
                                            <img src="__PUBLIC__/caidongweb/images/{if $data.go_recom == 1}tj1.png{else}tj2.png{/if}" onclick="changeTableValGoods('goods','go_id={$data.go_id}','go_recom','tj',this)"/>
                                            <img src="__PUBLIC__/caidongweb/images/{if $data.go_new == 1}xp1.png{else}xp2.png{/if}" onclick="changeTableValGoods('goods','go_id={$data.go_id}','go_new','xp',this)"/>
                                            <img src="__PUBLIC__/caidongweb/images/{if $data.go_hot == 1}cu1.png{else}cu2.png{/if}" onclick="changeTableValGoods('goods','go_id={$data.go_id}','go_hot','cu',this)"/>
                                            <img src="__PUBLIC__/caidongweb/images/{if $data.go_free == 1}by1.png{else}by2.png{/if}" onclick="changeTableValGoods('goods','go_id={$data.go_id}','go_free','by',this)"/>
                                        </p>
                                    </dd>
                                </dl>
                            </td>
                            <td><p>现金：{$data.go_price}</p><p>积分：{$data.go_score}</p></td>
                            <td><input type="number" value="{$data.go_stock}" onchange="ajaxUpFieldValue('goods','go_id={$data.go_id}','go_stock',this);" style="border-radius:5px;text-align: center"></td>
                            <td>
                                <!--<a title="查看"  onclick="x_admin_show('编辑','order-view.html')" href="javascript:;">
                                    <i class="layui-icon">&#xe63c;</i>
                                </a>-->
                                <a title="修改" onclick="cha('{$data.go_id}')" href="javascript:;">
                                    <i class="layui-icon">&#xe642;</i>
                                </a>
                                <a title="删除" onclick="del(this,'{$data.go_id}')" href="javascript:;">
                                    <i class="layui-icon">&#xe640;</i>
                                </a>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        {$page}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var form = layui.form;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    });
    //修改指定表的指定字段值:商品列表使用
    function changeTableValGoods(table,where,field,img,obj){
        var src = "";
        if($(obj).attr('src').indexOf(img+"2.png") > 0 ){
            src = '/Public/caidongweb/images/'+img+'1.png';
            var value = 1;
        }else{
            src = '/Public/caidongweb/images/'+img+'2.png';
            var value = 2;
        }
        $.ajax({
            type : 'post',
            url:"/caidongweb/Goods/updateState",
            data : {table:table,where:where,field:field,value:value},
            success: function(data){
                $(obj).attr('src',src);
                ajax_get_table('myfrom1',cur_page);     //ajax无更新刷新
            }
        });
    }

    //修改指定列表字段的值【如：修改库存，列表排序】
    function ajaxUpFieldValue(table,where,field,obj)
    {
        var value = $(obj).val();
        //alert(value);
        $.ajax({
            type : 'post',
            url:"/caidongweb/Goods/updateState",
            data : {table:table,where:where,field:field,value:value},
            success: function(data){
                layer.msg('更新成功', {icon: 1});
                ajax_get_table('myfrom1',cur_page);
            }
        });
    }
    //永久删除
    function del(obj,id) {
        layer.confirm('确认要删除此商品吗？（一但删除不可恢复！！！）',function(index){
            $.ajax({
                type:'post',
                url:'{:url("Goods/goodDel")}',
                dataType: 'json',
                data:{id:id},
                success: function(data){
                    if(data.code > 0){
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!',{icon:1,time:1000});
                    }else{
                        layer.msg(data.msg,{icon:5,time:2000})
                    }
                }
            });
        });
    }

    //添加商品
    function edit(id = '')
    {
        var url = '{:url("Goods/good_add")}';
        var desc = '添加';
        var arr = ['80%','95%'];
        var loading =  layer.load(0, {shade: false});
        layer.open({
            type: 2,
            title:desc,
            area: arr,
            content: url
        });
        layer.close(loading);
    }

    //编辑商品
    function cha(id = '')
    {
        var url = '{:url("Goods/good_edit",["id"=>"getId"])}';
        url = url.replace('getId',id);
        var loading =  layer.load(0, {shade: false});
        layer.open({
            type: 2,
            title:'编辑',
            area: ['80%','80%'],
            content: url
        });
        layer.close(loading);
    }
</script>
</html>